package com.example.administrator.worldcup545;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.worldcup545.util.DB;


public class MainActivity extends AppCompatActivity {

    private ImageView fanhui;
    private EditText name;
    private EditText password;
    private EditText password2;
    private Button zhuche;
    private String nameStr;
    private String passwordStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        fanhui = (ImageView) findViewById(R.id.fanhui_im);
        name = (EditText) findViewById(R.id.user_name2_et);
        password = (EditText) findViewById(R.id.password2_et);
        password2 = (EditText) findViewById(R.id.password3_et);
        zhuche = (Button) findViewById(R.id.sign_in);
    }



    private void initData() {
        nameStr = name.getText().toString() + "";
        passwordStr = password.getText().toString() + "";
    }

    private void initEvent() {
        zhuche.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                if (name.getText().toString().length() >= 3) {
                    if (name.getText().toString().length() > 5 && password.getText().toString().length() <= 15) {
                        if (password2.getText().toString().length() == password.getText().toString().length()) {
                            Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(MainActivity.this, "两次密码不一样", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(MainActivity.this, "密码格式不符合要求", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "用户名不符合要求", Toast.LENGTH_SHORT).show();
                }
                DB db = new DB(MainActivity.this);
                db.signInInsert(nameStr, passwordStr);
            }
        });

        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });

    }
}